package com.tenglong.teacher.ui.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.tenglong.teacher.R;

public class ExitDialog extends DialogFragment implements View.OnClickListener {
    private TextView mTvContent,mTvCancle,mTvConfirm;
    private ExitListener listener;
    private static final String KEY_EXIT_CONTENT = ExitDialog.class.getSimpleName()+"---key_exit_title";


    public static ExitDialog getExitDialog(String title){
        if (TextUtils.isEmpty(title)){
            title = "数据错误";
        }
        ExitDialog dialog = new ExitDialog();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_EXIT_CONTENT,title);
        dialog.setArguments(bundle);
        return dialog;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); /** 去4.4,5.1空Title */
        return LayoutInflater.from(getContext()).inflate(R.layout.exit_dialog_layout,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        setListener();
    }

    private void setListener() {
        mTvCancle.setOnClickListener(this);
        mTvConfirm.setOnClickListener(this);
    }

    private void initView(View view) {
        mTvContent = view.findViewById(R.id.tv_content);
        mTvCancle = view.findViewById(R.id.tv_cancle);
        mTvConfirm = view.findViewById(R.id.tv_confirm);
        mTvContent.setText(getArguments().getString(KEY_EXIT_CONTENT));
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        getDialog().setCanceledOnTouchOutside(false);
        WindowManager.LayoutParams params = window.getAttributes();
//        params.gravity = Gravity.BOTTOM;
//        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(params);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    @Override
    public void onClick(View v) {
        dismiss();
        if (listener!=null){
            listener.onExit(v.getId()==R.id.tv_confirm);
        }
    }

    public ExitDialog setExitListener(ExitListener listener){
        this.listener = listener;
        return this;
    }

    public interface ExitListener{
        void onExit(boolean exitState);
    }
}
